// recommendCourse_package/pages/recommendCourse/recommendCourse.ts\
import {getRecommendCourseList} from '../../../api/recommedCourse';
Page({

  /**
   * 页面的初始数据
   */
  data: {
  title:"",
  recommendCourseList:[],
  selectList:[{id:0,name:"全部"},{id:1,name:"团课"},{id:2,name:"个训课"}], //选择列表
  defalutSelect:"",
  formData:{
    pageNum:1,
    pageSize:10,
    schoolId:wx.getStorageSync("schoolId"),
  },
  total:0, //总页数
  moreLimt:false,//是否加载完成
  isfreshComplete:false //是否刷新完成
  },
  // 列表刷新课程
  loadPageEvent(data:any){
  const type = data.detail.type
  switch(type){
    case 'refresh':
      this.resetPage()
      this.getRecommendCourseList()
      this.setData({
        isfreshComplete:true,
        recommendCourseList:[]
      })
      break;
     case 'load':
      let formData = this.data.formData as any
      if(formData.pageNum * formData.pageSize < this.data.total){
        formData.pageNum  = formData.pageNum + 1
        this.setData({
          formData
        })
        this.getRecommendCourseList()
      }else{
       this.setData({
        moreLimt: true
       })
      }
      break;
  }
  

  },
  // 重置页码
  resetPage(){
    let formData = this.data.formData as any
    formData.pageNum = 1
   this.setData({
    formData
   })
  },
  // 查看课程详情
  seeCourseDetail(event:any){    
    const {title,courseserialid} =event.currentTarget.dataset
    wx.navigateTo({
      url:`/recommendCourse_package/pages/CourseIntroduction/CourseIntroduction?title=${title}&serialId=${courseserialid}`
    })
  },
  // 筛选课程
  dropSelect(e:any){
    this.resetPage()
    if(e.detail !== 0){
      const formData= {...this.data.formData,courseType:e.detail}
      this.setData({
       formData,
       recommendCourseList:[]
      })
    }else{
      const formData= this.data.formData as any
      delete formData.courseType
      this.setData({
       formData,
       recommendCourseList:[]
      })
    }

   this.getRecommendCourseList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(data:any) {
    const {type} = data
    this.setData({
      title:type
    })
    this.getRecommendCourseList()
  },
  // 搜索课程
  searchList(e:any){
  this.resetPage()
  const {value} = e.detail
  if(value && value !== ""){
   const formData = {...this.data.formData,search:value}
   this.setData({
    formData,
    recommendCourseList:[]
   })
  }else{
    const formData = this.data.formData as any
    delete formData.search
    this.setData({
      formData,
      recommendCourseList:[]
     })

  }
  this.getRecommendCourseList()

  },
  // 获取推荐课程列表
  async getRecommendCourseList(){
  // 查看是否是推荐课程还是全部课程
  if(this.data.title === "推荐课程"){
    const formData = {...this.data.formData,isRecommend:1}
   this.setData({
    formData
     })
  }
  const {code, data:res} = await getRecommendCourseList(this.data.formData)
  if(code === 200){
    const {list,total} = res;
    
    let recommendCourseList = this.data.recommendCourseList.concat(list)
    this.setData({
      recommendCourseList,
      total
    })
  }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})